package com.csi.controller.user;

import com.alibaba.fastjson2.JSON;
import com.csi.domain.User;
import com.csi.pojo.Result;
import com.csi.service.UserService;
import com.csi.service.impl.UserServiceImpl;
import com.csi.utils.MD5Tools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user/updatePasswordController")
public class UpdatePasswordController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 设置响应内容类型
        resp.setContentType("application/json;charset=utf-8");

        // 获取请求参数
        int userId = Integer.parseInt(req.getParameter("userId"));
        String oldPassword = req.getParameter("oldPassword");
        String newPassword = req.getParameter("newPassword");
        String againNewPassword = req.getParameter("againNewPassword");

        // 验证参数格式是否正确
        if (!isValidPassword(oldPassword) || !isValidPassword(newPassword) || !isValidPassword(againNewPassword)) {
            // 返回错误信息
            Result<String> result = new Result<>();
            result.setCode(400);
            result.setMsg("密码格式不正确");
            result.setDatas(null);
            PrintWriter out = resp.getWriter();
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        // 判断oldPassword加盐加密后与数据库密码是否相等
        UserService userService = new UserServiceImpl();
        User user = userService.findById(userId);
        String salt = userService.findSalt(userId);
        String encryptedOldPassword = MD5Tools.MD5Encode(oldPassword + salt,"utf-8");
        if (!encryptedOldPassword.equals(user.getLoginPassword())) {
            // 如果旧密码不正确，则返回错误信息
            Result<String> result = new Result<>();
            result.setCode(400);
            result.setMsg("旧密码不正确");
            result.setDatas(null);
            PrintWriter out = resp.getWriter();
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        // 判断新密码和确认密码是否相等
        if (!newPassword.equals(againNewPassword)) {
            // 如果新密码和确认密码不相等，则返回错误信息
            Result<String> result = new Result<>();
            result.setCode(400);
            result.setMsg("新密码和确认密码不相等");
            result.setDatas(null);
            PrintWriter out = resp.getWriter();
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        // 判断新密码是否与旧密码相同
        if (newPassword.equals(oldPassword)) {
            // 如果新密码与旧密码相同，则返回错误信息
            Result<String> result = new Result<>();
            result.setCode(400);
            result.setMsg("新密码不能与旧密码相同");
            result.setDatas(null);
            PrintWriter out = resp.getWriter();
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        // 更新密码
        userService.updatePassword(userId, newPassword);

        // 返回成功信息
        Result<String> result = new Result<>();
        result.setCode(200);
        result.setMsg("密码修改成功");
        result.setDatas(null);
        PrintWriter out = resp.getWriter();
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }

    // 验证密码格式是否正确
    private boolean isValidPassword(String password) {
        String passwordReg = "^\\w{6,12}$"; // 密码正则表达式
        return password.matches(passwordReg);
    }

}
